package spark.core.scala

import java.util.Arrays

import org.apache.spark.{SparkContext, Accumulator, SparkConf}

/**
 * 累加变量
 */
object AccumulatorVariable {

  def main(args: Array[String]) {
    val conf = new SparkConf()
      .setMaster("local")
      .setAppName("AccumulatorVariable")
    val sc = new SparkContext(conf)
    val sum: Accumulator[Int] = sc.accumulator(0)
    val numberList = Array(1, 2, 3, 4, 5)
    val numbers = sc.parallelize(numberList, 1)
    numbers.foreach(x => sum += x)
    println(sum)
  }

}
